package com.chima.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.chima.domain.DetailJob;
import com.chima.domain.Job;
import com.chima.domain.TastJob;

public class TastJobDao extends HibernateDaoSupport{
	
	//分页查询
		public List<TastJob> findByCriteria(DetachedCriteria criteria, int firstResult,int maxResults) {
			return this.getHibernateTemplate().findByCriteria(criteria,firstResult,maxResults);
		}
	   //查找总页数
		public int findByTotalCount( String date, int emp)  {
			long temp=2312312l;
			if(emp==0){
				if(date==null){
					 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
			         Calendar   calendar=Calendar.getInstance(); 
			         calendar.setTime(new Date());
			         date= sdf.format(calendar.getTime());
					List list= this.getHibernateTemplate().find("select count(*) from TastJob where YEAR(creatDate)=YEAR('"+date+"') ");
					 temp = (Long) list.get(0);
				}else if(date!=null){
					 int day=Integer.parseInt( date.substring(5,7));
					 int year=Integer.parseInt( date.substring(0,4));
					 String  First =getFirstDayOfMonth(year,day);
					 String  last =getLastDayOfMonth(year,day);
					List list= this.getHibernateTemplate().find("select count(*) from TastJob where creatDate >= '"+First+"' and creatDate <= '"+last+"' ");
					 temp = (Long) list.get(0);
				}
			   
			}else if(emp!=0){
				if(date==null){
					 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
			         Calendar   calendar=Calendar.getInstance(); 
			         calendar.setTime(new Date());
			         date= sdf.format(calendar.getTime());
					List list= this.getHibernateTemplate().find("select count(*) from TastJob where YEAR(creatDate)=YEAR('"+date+"') and employee.id="+emp+" ");
					 temp = (Long) list.get(0);
				}else if(date!=null){
					 int day=Integer.parseInt( date.substring(5,7));
					 int year=Integer.parseInt( date.substring(0,4));
					 String  First =getFirstDayOfMonth(year,day);
					 String  last =getLastDayOfMonth(year,day);
					List list= this.getHibernateTemplate().find("select count(*) from TastJob where creatDate >= '"+First+"' and creatDate <= '"+last+"' and employee.id="+emp+"  ");
					 temp = (Long) list.get(0);
				}
			}
			
			return (int) temp;
		}
		
		
		public void save( TastJob  tastJob) {
			this.getHibernateTemplate().save( tastJob);
		}
		public void update(TastJob  tastJob) {
			this.getHibernateTemplate().update( tastJob);
		}
		
		public void delete(  TastJob  tastJob) {
			this.getHibernateTemplate().delete( tastJob);
			
		}

		public TastJob findById(int id) {
		    Session session=this.getSession();
		    TastJob j=(TastJob) session.createQuery(" from TastJob  where id=?").setParameter(0,id).uniqueResult();
			super.releaseSession(session);
			return j;
		}
		 /***
		  * 得到某年某月的第一天
		  * @param year
		  * @param month
		 * @throws ParseException 
		 */
		public String getFirstDayOfMonth(int year, int month)   {
			 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
			  Calendar cal = Calendar.getInstance();
			  cal.set(Calendar.YEAR, year);
			  cal.set(Calendar.MONTH, month-1);
			  cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
			  String first=new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()); 
				return first;
			
		}
	  /**
		  * 得到某年某月的最后一天
		  *
		  * @param year
		  * @param month
		*/
		 public  String getLastDayOfMonth(int year, int month) {
			 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
			  Calendar cal = Calendar.getInstance();
			  cal.set(Calendar.YEAR, year);
			  cal.set(Calendar.MONTH, month-1);
			  cal.set(Calendar.DAY_OF_MONTH, 1);
			  int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
			  cal.set(Calendar.DAY_OF_MONTH, value);
			 String last=new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()); 
			
			  return last ;
		 }

}
